from conans import ConanFile, CMake, tools, AutoToolsBuildEnvironment
from conans import tools

import sys
import os
sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path
import shutil


class LibeventConan(ConanFile):
    name = "OPENSSL"
    version = "1.1.1"
    url = "https://gitee.com/mirrors/openssl.git"
    description = "OPENSSL"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake_find_package"

    def source(self):
        my_file = Path("./pkgsrc")
        if my_file.exists():
            print("not need to update source")
        else:
            print("clone from " ,self.url,"branch OpenSSL_1_1_0-stable" )
            tools.rmdir("./pkgsrc")
            git = tools.Git(folder="./pkgsrc", verify_ssl=False)
            git.clone(self.url,branch="OpenSSL_1_1_0-stable")
    def build(self):
        print("path " + self.source_folder + " is where the package built")
        if tools.os_info.is_windows:
            print("current os windows")
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                if tools.get_env("CONAN_SH_PATH") == "":
                    print("msys openssl build require sh.exe in the CONAN_SH_PATH enviorment")
                shutil.copy("sh.exe", tools.get_env("CONAN_SH_PATH"))
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True,
                                              bashcmd="perl Configure mingw -DOPENSSL_NO_MD2  --prefix=" + tools.unix_path(self.package_folder))
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True, bashcmd="mingw32-make -j 16")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True, bashcmd="mingw32-make install")
                os.remove(tools.get_env("CONAN_SH_PATH"))  # path是文件的路径，如果这个路径是一个文件夹，则会抛出OSError的错误，这时需用用rmdir()来删除
            if tools.get_env("BUILD_ENV") == "msvc32":
                env_vars = tools.vcvars_dict(self)
                with tools.environment_append(env_vars):
                    self.run("perl Configure VC-WIN32 no-asm  --prefix=" + self.package_folder ,True,cwd="pkgsrc")
                    self.run("nmake -",cwd="pkgsrc")
                    self.run("nmake install_sw",cwd="pkgsrc")
            if tools.get_env("BUILD_ENV") == "msvc64":
                env_vars = tools.vcvars_dict(self)
                print(env_vars)
                with tools.environment_append(env_vars):
                    self.run("perl Configure VC-WIN64A   no-asm  --prefix=" + self.package_folder, True, cwd="pkgsrc")
                    self.run("nmake ",cwd="pkgsrc")
                    self.run("nmake install_sw",cwd="pkgsrc")

            if tools.os_info.is_linux:
                print("current os linux ")
                self.run("./Configure DOPENSSL_NO_MD2 --prefix=" + tools.unix_path(self.package_folder),cwd="pkgsrc")
                self.run("make -j 8",cwd="pkgsrc")
                self.run("make install",cwd="pkgsrc")



    def export(self):
        print("msys build the autoconf needs sh.exe normally it is not in path")
        self.copy("sh.exe",dst="",keep_path=True)

    def requirements(self):
        if tools.os_info.is_windows:
            self.requires("nasm/2.15.05")
            if tools.get_env("BUILD_ENV") == "msys":
                self.requires("msysperl/5.30.0.1")
            if tools.get_env("BUILD_ENV") == "msvc32":
                self.requires("strawberryperl/5.30.0.1")
            if tools.get_env("BUILD_ENV") == "msvc64":
                self.requires("strawberryperl/5.30.0.1")                
    def package_info(self):
        print(self.settings.compiler)
        print("Openssl package info")
        self.cpp_info.name = 'OPENSSL'
        if tools.os_info.is_windows:
            print("Openssl package info ", tools.get_env("BUILD_ENV"))
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if self.settings.compiler == "gcc":
                print("Openssl windows msys package")
                self.cpp_info.libdirs = ['lib'] 
                self.cpp_info.libs.append("libssl.a") 
                self.cpp_info.libs.append("libcrypto.a") 
                self.cpp_info.names["cmake_find_package"] = 'OPENSSL'

            if tools.get_env("BUILD_ENV") == "msvc":
                print("msvc build")
                if tools.get_env("BUILD_ENV") == "msvc32":
                    self.cpp_info.names["cmake_find_package"] = "zlib"
                    self.cpp_info.names["cmake_find_package_multi"] = "zlib"
                    self.cpp_info.libdirs = ['lib']  # Directories where libraries can be found
                    self.cpp_info.libs.append("libssl_static.a") 
                    self.cpp_info.libs.append("libtestutil_static.a") 
                    self.cpp_info.libs.append("libcrypto_static.a") 
                    self.cpp_info.libs.append("liblegacy_static.a") 



